package com.lvt.example.thinking.in.spring.bean.lifecycle;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;

/**
 * desc：{@link org.springframework.context.annotation.AnnotatedBeanDefinitionReader} 元信息解析示例
 *
 * @author LT
 * @date 2022/4/5
 */
@Slf4j
public class AnnotatedBeanDefinitionReaderParsingDemo {

    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        AnnotatedBeanDefinitionReader beanDefinitionReader = new AnnotatedBeanDefinitionReader(beanFactory);
        // 注册前
        int before = beanFactory.getBeanDefinitionCount();
        beanDefinitionReader.registerBean(AnnotatedBeanDefinitionReaderParsingDemo.class);
        int after = beanFactory.getBeanDefinitionCount();
        log.info("已加载 BeanDefinition 数量：{} 个", (after-before));

        // 根据名称和类型查找
        // Bean 名称来自 BeanNameGenerator, 注解 AnnotationBeanNameGenerator
        AnnotatedBeanDefinitionReaderParsingDemo demo = beanFactory.getBean("annotatedBeanDefinitionReaderParsingDemo",
                AnnotatedBeanDefinitionReaderParsingDemo.class);
        log.info("demo 实例：{}", demo);
    }
}
